﻿using Analysis.Models.EchartModel;
using Bll;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Utils;

namespace Analysis.Controllers
{
    public class ProductController : Controller
    {
        public static int pageSize = ConfigurationManager.AppSettings["PageSize"].ConToInt();
        // GET: Product
        public ActionResult Index()
        {
            //if (Session["User"] == null)
            //{
            //    return RedirectToAction("Index", "LoginManager");
            //}
            DateTime start = DateTime.Now;
            LogHelper.WriteInfoLog("商品列表页进入时间=" + start.ToString("yyyy-MM-dd HH:mm:ss"));
            return View();
        }

        public PartialViewResult _ProductList()
        {
            string beginTime = Request["beginTime"];
            string endTime = Request["endTime"];
            string plateformType = Request["plateformType"] == null ? "-1" : Request["plateformType"]; //-1：全部 0：实体店 2：电商
            string productName = Request["productName"];
            int page = Request["page"] == null ? 1 : Request["page"].ConToInt();


            beginTime = Extend.ConToBeginTime(beginTime);
            endTime = Extend.ConToEndTime(endTime);

            List<ProductModel> proList = new List<ProductModel>();

            List<long> productIds = new List<long>();
            List<long> productIdsForBBC = new List<long>();
            List<long> productIdsForSYJ = new List<long>();
            IList<SelectProductModelForSYJ> productModelForSYJ = new List<SelectProductModelForSYJ>();
            IList<SelectProductModelForBBC> productModelForBBC = new List<SelectProductModelForBBC>();

            #region 查询商品列表
            string sql_syj_product_list = "select sx# as sx,na from t_item_info where 1=1";
            string sql_bbc_product_list = "select id,productName,syjNo from product where isDelete=0 and status='online' and displayUnits=1";
            if (!string.IsNullOrEmpty(productName))
            {
                sql_syj_product_list += " and na like '%" + productName + "%'";
                sql_bbc_product_list += " and productName like '%" + productName + "%'";
            }

            if (plateformType == "-1")
            {
                productModelForSYJ = BllSYJCommon.SelectEntityBySql<SelectProductModelForSYJ>("t_item_info", sql_syj_product_list);
                productModelForBBC = BllBBCCommon.SelectEntityBySql<SelectProductModelForBBC>("Product", sql_bbc_product_list);
                if (productModelForSYJ != null)
                {
                    productIdsForSYJ = productModelForSYJ.Select(x => x.sx.ConToLong()).ToList();
                    productIds.AddRange(productIdsForSYJ);
                }
                if (productModelForBBC != null)
                {
                    productIdsForBBC = productModelForBBC.Select(x => x.id).ToList();
                    productIds.AddRange(productIdsForBBC);
                }
            }
            else if (plateformType == "0")   //实体店
            {
                productModelForSYJ = BllSYJCommon.SelectEntityBySql<SelectProductModelForSYJ>("t_item_info", sql_syj_product_list);
                if (productModelForSYJ != null)
                {
                    productIdsForSYJ = productModelForSYJ.Select(x => x.sx.ConToLong()).ToList();
                    productIds.AddRange(productIdsForSYJ);
                }
            }
            else if (plateformType == "1")  //电商
            {
                productModelForBBC = BllBBCCommon.SelectEntityBySql<SelectProductModelForBBC>("Product", sql_bbc_product_list);
                if (productModelForBBC != null)
                {
                    productIdsForBBC = productModelForBBC.Select(x => x.id).ToList();
                    productIds.AddRange(productIdsForBBC);
                }
            }
            #endregion

            #region 总额,商品数,库存
            string productIdBBCStr = string.Join(",", productIdsForBBC);
            string productIdSYJstr = string.Join(",", productIdsForSYJ);
            string sql_syj_in_product = null;
            string sql_bbc_in_product = null;

            if (string.IsNullOrEmpty(productIdSYJstr))
            {
                 sql_syj_in_product = "in (0)";
            }
            else
            {
                sql_syj_in_product = "in (" + productIdSYJstr + ")";
            }
            if (string.IsNullOrEmpty(productIdBBCStr))
            {
                 sql_bbc_in_product = "in (0)";
            }
            else
            {
                sql_bbc_in_product = "in (" + productIdBBCStr + ")";
            }


            List<ProductTotal> syjProductTotalList = new List<ProductTotal>();
            List<ProductAmount> syjProductAmountList = new List<ProductAmount>();
            List<ProductTotal> bbcProductTotalList = new List<ProductTotal>();
            List<ProductAmount> bbcProductAmountList = new List<ProductAmount>();
            List<ProductCodeAndNum> codeAndStore = new List<ProductCodeAndNum>();
            List<ProductIdAndStore> productIdAndStore = new List<ProductIdAndStore>();
             List<ProductIdAndCode> codeList= new List<ProductIdAndCode>();

            if (plateformType == "-1")
            {
                syjProductTotalList = SYJGetProductTotal(beginTime, endTime, sql_syj_in_product);
                syjProductAmountList = SYJGetProductAmount(beginTime, endTime, sql_syj_in_product);

                bbcProductTotalList = BBCGetProductTotal(beginTime, endTime, sql_bbc_in_product);
                bbcProductAmountList = BBCGetProductAmount(beginTime, endTime, sql_bbc_in_product);

                codeAndStore = BBCGetCodeAndNum(sql_bbc_in_product);
                productIdAndStore = SYJGetProductIdAndStore(sql_syj_in_product);
                codeList=BBCGetCodeList(sql_bbc_in_product);

            }
            else if (plateformType == "0")   //实体店
            {
                syjProductTotalList = SYJGetProductTotal(beginTime, endTime, sql_syj_in_product);
                syjProductAmountList = SYJGetProductAmount(beginTime, endTime, sql_syj_in_product);
                productIdAndStore = SYJGetProductIdAndStore(sql_syj_in_product);
            }
            else if (plateformType == "1")  //电商
            {
                bbcProductTotalList = BBCGetProductTotal(beginTime, endTime, sql_bbc_in_product);
                bbcProductAmountList = BBCGetProductAmount(beginTime, endTime, sql_bbc_in_product);
                codeAndStore = BBCGetCodeAndNum(sql_bbc_in_product);
                codeList = BBCGetCodeList(sql_bbc_in_product);
            }
            #endregion

            #region 循环
            foreach (long productId in productIds)
            {
                ProductTotal syjTotal = null;
                ProductAmount syjAmount = null;
                ProductTotal bbcTotal = null;
                ProductAmount bbcAmount = null;
                if (syjProductTotalList != null)
                {
                    syjTotal = syjProductTotalList.Where(x => x.productId == productId).FirstOrDefault();
                }
                if (syjProductAmountList != null)
                {
                    syjAmount = syjProductAmountList.Where(x => x.productId == productId).FirstOrDefault();
                }
                if (bbcProductTotalList != null)
                {
                    bbcTotal = bbcProductTotalList.Where(x => x.productId == productId).FirstOrDefault();
                }
                if (bbcProductAmountList != null)
                {
                    bbcAmount = bbcProductAmountList.Where(x => x.productId == productId).FirstOrDefault();
                }

                int product_all_order_num = 0;
                decimal product_all_sale_amount = 0;
                decimal cost_per_order = 0;
                if (plateformType == "-1")
                {
                    //实体店订单数
                    int product_shop_order_num = syjAmount == null ? 0 : syjAmount.amount.ConToInt();
                    //线上订单数
                    int product_online_order_num = bbcAmount == null ? 0 : bbcAmount.amount.ConToInt();
                    //总订单数
                    product_all_order_num = product_shop_order_num + product_online_order_num;

                    //实体店销售额
                    decimal product_shop_sale_amount = syjTotal == null ? 0 : syjTotal.total;
                    //线上销售额
                    decimal product_online_sale_amount = bbcTotal == null ? 0 : bbcTotal.total;
                    //总销售额
                    product_all_sale_amount = product_shop_sale_amount + product_online_sale_amount;
                }
                else if (plateformType == "0")   //实体店
                {
                    //实体店订单数
                    int product_shop_order_num = syjAmount == null ? 0 : syjAmount.amount.ConToInt();
                    //总订单数
                    product_all_order_num = product_shop_order_num;

                    //实体店销售额
                    decimal product_shop_sale_amount = syjTotal == null ? 0 : syjTotal.total;
                    //总销售额
                    product_all_sale_amount = product_shop_sale_amount;
                }
                else if (plateformType == "1")  //电商
                {
                    //线上订单数
                    int product_online_order_num = bbcAmount == null ? 0 : bbcAmount.amount.ConToInt();
                    //总订单数
                    product_all_order_num = product_online_order_num;

                    //线上销售额
                    decimal product_online_sale_amount = bbcTotal == null ? 0 : bbcTotal.total;
                    //总销售额
                    product_all_sale_amount = product_online_sale_amount;
                }
                if (product_all_order_num > 0)
                {
                    cost_per_order = Math.Round(product_all_sale_amount / product_all_order_num, 2);
                }

                ProductModel model = new ProductModel();
                model.ProductId = productId;


                SelectProductModelForSYJ temp = null;
                SelectProductModelForBBC temp2 = null;
                if (productModelForSYJ != null)
                {
                   temp = productModelForSYJ.Where(x => x.sx == productId).FirstOrDefault();
                }
                if (productModelForBBC != null)
                {
                    temp2 = productModelForBBC.Where(x => x.id == productId).FirstOrDefault();
                }

                string productName_ = "";
                int type = 0;
                int storeNum = 0;
                long syjNo = 0;
                if (plateformType == "-1")
                {
                    if (temp == null)
                    {
                        //电商
                        productName_ = temp2.productName;
                        type = 1;
                        string code = codeList.Where(x => x.id == productId).FirstOrDefault().articlenumber;
                        var codeAndStoreTemp = codeAndStore.Where(x => x.code == code).FirstOrDefault();
                        if (codeAndStoreTemp != null)
                        {
                            storeNum = (int)codeAndStoreTemp.nums;
                        }
                        syjNo = productModelForBBC.Where(x => x.id == productId).First().syjNo.ConToLong();
                    }
                    else
                    {
                        productName_ = temp.na;
                        type = 0;
                        var productIdAndStoreTemp = productIdAndStore.Where(x => x.ProductId == productId).FirstOrDefault();
                        if (productIdAndStoreTemp != null)
                        {
                            decimal storeNum_ = productIdAndStoreTemp.storeNum;
                            storeNum = (int)storeNum_;
                        }
                    }
                }
                else if (plateformType == "0")   //实体店
                {
                    productName_ = temp.na;
                    type = 0;
                    var productIdAndStoreTemp = productIdAndStore.Where(x => x.ProductId == productId).FirstOrDefault();
                    if (productIdAndStoreTemp != null)
                    {
                        decimal storeNum_ = productIdAndStoreTemp.storeNum;
                        storeNum = (int)storeNum_;
                    }
                }
                else if (plateformType == "1")  //电商
                {
                    productName_ = temp2.productName;
                    type = 1;
                    string code = codeList.Where(x => x.id == productId).FirstOrDefault().articlenumber;
                    var codeAndStoreTemp = codeAndStore.Where(x => x.code == code).FirstOrDefault();
                    if (codeAndStoreTemp != null)
                    {
                        storeNum = (int)codeAndStoreTemp.nums;
                    }
                    syjNo = productModelForBBC.Where(x => x.id == productId).First().syjNo.ConToLong();
                }

                model.ProductName = productName_;
                model.CostPerOrder = cost_per_order;
                model.CountOrder = product_all_order_num;
                model.Total = product_all_sale_amount;
                model.PlateFormType = type;
                model.StoreNum = storeNum;
                model.SYJNo = syjNo;


                proList.Add(model);
            }
            #endregion

            if (plateformType == "-1")
            {
                #region 全部（要整合线上线下）
                List<ProductModel> proList2 = new List<ProductModel>();
                proList2.AddRange(proList.Where(x => x.PlateFormType == 1));  //所有电商的
                var temp = proList2.Select(x => x.SYJNo).Where(x => x != 0).ToList();  //所有线上线下都拥有的商品

                List<ProductModel> proList3 = proList.Where(x => x.PlateFormType == 0).ToList();

                foreach (ProductModel model in proList3)  //线下商品
                {
                    if (temp.Contains(model.ProductId))
                    {
                        ProductModel origin = proList2.Where(x => x.SYJNo == model.ProductId).First();
                        origin.CountOrder += model.CountOrder;
                        origin.Total += model.Total;
                        origin.PlateFormType = -1;

                        if (origin.CountOrder > 0)
                        {
                            origin.CostPerOrder = Math.Round(origin.Total / origin.CountOrder, 2);
                        }
                        else
                        {
                            origin.CostPerOrder = 0;
                        }
                        origin.StoreNum += model.StoreNum;
                    }
                    else
                    {
                        proList2.Add(model);
                    }
                }
                #endregion

                proList2 = proList2.Where(x => x.ProductName != "配送费" && x.ProductName != "餐盒").OrderByDescending(x => x.Total).ToList();
                int maxPage = Math.Ceiling(proList2.Count().ConToDouble() / pageSize.ConToDouble()).ConToInt();
                proList2=Extend.QueryByPage<ProductModel>( pageSize,page, proList2);
                ViewBag.proList = proList2;
                ViewBag.maxPage = maxPage;
            }
            else
            {
                proList = proList.Where(x => x.ProductName != "配送费" && x.ProductName != "餐盒").OrderByDescending(x => x.Total).ToList();
                int maxPage =Math.Ceiling(proList.Count().ConToDouble()/pageSize.ConToDouble()).ConToInt();
                proList=Extend.QueryByPage<ProductModel>(pageSize, page, proList);
                ViewBag.proList = proList;
                ViewBag.maxPage = maxPage;
            }
            ViewBag.curPage = page;
            ViewBag.pageSize = pageSize;

            DateTime end = DateTime.Now;
            LogHelper.WriteInfoLog("商品列表页结束时间=" + end.ToString("yyyy-MM-dd HH:mm:ss"));

            return PartialView();
        }

      

        #region Index查询所需类
        private List<ProductAmount> SYJGetProductAmount(string beginTime, string endTime, string sql_syj_in_product)
        {
            string sql_syj_all_product_count = "select Cast(ss.p# as bigint) as productId,cast(count(ss.p#) as bigint) as amount from t_pos_syd as sd,t_pos_sys as ss where sd.s#=ss.d# and  sd.ct>='" + beginTime + "' and sd.ct<='" + endTime + "' and ss.p# " + sql_syj_in_product + " and sd.yss#=0 and sd.s# not in(select yss# from t_pos_syd where yss#>0 and ct>='"+beginTime+"' and ct<='"+endTime+"')  group by ss.p#";
            List<ProductAmount> syjProductAmountList = BllSYJCommon.SelectEntityBySql<ProductAmount>("t_pos_syd", sql_syj_all_product_count);
            return syjProductAmountList;
        }
        private List<ProductTotal> SYJGetProductTotal(string beginTime, string endTime, string sql_syj_in_product)
        {
            string sql_syj_all_product_total = "select Cast(ss.p# as bigint) as productId,isnull(sum(sd.dcpayment-sd.dcchange),0) as total from t_pos_syd as sd,t_pos_sys as ss where sd.s#=ss.d# and  sd.ct>='" + beginTime + "' and sd.ct<='" + endTime + "' and ss.p# " + sql_syj_in_product + "  group by ss.p#";
            List<ProductTotal> syjProductTotalList = BllSYJCommon.SelectEntityBySql<ProductTotal>("t_pos_syd", sql_syj_all_product_total);
            return syjProductTotalList;
        }

        private List<ProductAmount> BBCGetProductAmount(string beginTime, string endTime, string sql_bbc_in_product)
        {
            string sql_bbc_all_product_count = "select od.productId,cast(count(od.productId) as bigint) as amount from orders as o,orderDetail as od where o.id=od.orderId and o.payStatusId=2 and  o.payTime>='" + beginTime + "' and o.payTime<='" + endTime + "' and od.productId " + sql_bbc_in_product + " and o.plateform=3 group by od.productId";
            List<ProductAmount> bbcProductAmountList = BllBBCCommon.SelectEntityBySql<ProductAmount>("orders", sql_bbc_all_product_count);
            return bbcProductAmountList;
        }
        private List<ProductTotal> BBCGetProductTotal(string beginTime, string endTime, string sql_bbc_in_product)
        {
            string sql_bbc_all_product_total = "select od.productId,isnull(sum(o.realTotal),0) as total from orders as o,orderDetail as od where o.id=od.orderId and o.payStatusId=2 and  o.payTime>='" + beginTime + "' and o.payTime<='" + endTime + "' and od.productId " + sql_bbc_in_product + " and o.plateform=3 group by od.productId";
            List<ProductTotal> bbcProductTotalList = BllBBCCommon.SelectEntityBySql<ProductTotal>("orders", sql_bbc_all_product_total);
            return bbcProductTotalList;
        }
        private List<ProductIdAndStore> SYJGetProductIdAndStore(string sql_syj_in_product)
        {
            string sql_syj_all_product_store = "select Cast(s.ks# as bigint) as productId,s.yl as storeNum from t_item_storage as s,t_item_info as f where s.ks#=f.sx# and s.ks# " + sql_syj_in_product;
            List<ProductIdAndStore> productIdAndStore = BllSYJCommon.SelectEntityBySql<ProductIdAndStore>("t_item_storage", sql_syj_all_product_store);
            return productIdAndStore;
        }

        private List<ProductIdAndCode> BBCGetCodeList(string sql_bbc_in_product)
        {
            string sql_bbc_all_product_code = "select id,articlenumber from product where id " + sql_bbc_in_product;
            List<ProductIdAndCode> codeList = BllBBCCommon.SelectEntityBySql<ProductIdAndCode>("product", sql_bbc_all_product_code);
            return codeList;
        }
        private List<ProductCodeAndNum> BBCGetCodeAndNum(string sql_bbc_in_product)
        {
            List<ProductCodeAndNum> codeAndStore = new List<ProductCodeAndNum>();

            List<ProductIdAndCode> codeList = BBCGetCodeList(sql_bbc_in_product);

            string codeStr = "";
            foreach (ProductIdAndCode temp in codeList)
            {
                if (string.IsNullOrEmpty(codeStr))
                {
                    codeStr = "'" + temp.articlenumber + "'";
                }
                else
                {
                    codeStr += ",'" + temp.articlenumber + "'";
                }
            }
            if (!string.IsNullOrEmpty(codeStr))
            {
                string sql_erp_all_product_store = "select  s.`code`,r.nums from is_room_info as r,is_sp_info as s where r.sp_id=s.id and s.`code` in(" + codeStr + ")";
                codeAndStore = BllERPCommon.SelectEntityBySql<ProductCodeAndNum>("is_room_info", sql_erp_all_product_store);
            }
            return codeAndStore;
        }
        private class SelectProductModelForBBC
        {
            public long id { get; set; }

            public string productName { get; set; }

            public string syjNo { get; set; }
        }

        private class SelectProductModelForSYJ
        {
            public int sx { get; set; }

            public string na { get; set; }
        }

        public class ProductModel
        {
            /// <summary>
            /// 0：实体店 1：电商
            /// </summary>
            public int PlateFormType { get; set; }
            public long ProductId { get; set; }
            /// <summary>
            /// 产品名称
            /// </summary>
            public string ProductName { get; set; }
            /// <summary>
            /// 客户单笔消费额
            /// </summary>
            public decimal CostPerOrder { get; set; }
            /// <summary>
            /// 下单笔数
            /// </summary>
            public int CountOrder { get; set; }
            /// <summary>
            /// 营业统计
            /// </summary>
            public decimal Total { get; set; }
            /// <summary>
            /// 实时库存
            /// </summary>
            public int StoreNum { get; set; }

            public long SYJNo { get; set; }
        }

        private class ProductTotal
        {
            public long productId { get; set; }

            public decimal total { get; set; }
        }
        private class ProductAmount
        {
            public long productId { get; set; }

            public long amount { get; set; }
        }

        private class ProductIdAndCode
        {
            public long id { get; set; }

            public string articlenumber { get; set; }
        }

        private class ProductCodeAndNum
        {
            public string code { get; set; }

            public long nums { get; set; }
        }

        private class ProductIdAndStore
        {
            public long ProductId { get; set; }

            public decimal storeNum { get; set; }
        }
        #endregion

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="plateformType"></param>
        /// <param name="isToday"></param>
        /// <returns></returns>
        public ActionResult Detail(long productId, string productName, string plateformType, string beginTime, string endTime, long syjNo = 0)
        {
            //if (Session["User"] == null)
            //{
            //    return RedirectToAction("Index", "LoginManager");
            //}

            DateTime start = DateTime.Now;
            LogHelper.WriteInfoLog("商品详情页进入时间=" + start.ToString("yyyy-MM-dd HH:mm:ss"));


            beginTime = Extend.ConToBeginTime(beginTime);
            endTime = Extend.ConToEndTime(endTime);


            //销售金额
            decimal single_product_total = 0;
            //BBC销售数量
            int single_product_bbc_count = 0;
            //SYJ销售数量
            int single_product_syj_count = 0;
            //SYJ销售单位
            string single_product_syj_unit = "";
            //订单数
            int single_product_order_count = 0;
            //单笔消费额
            decimal single_product_cost_per_order = 0;
            //当前库存
            int single_product_store = 0;
            //浏览量
            int single_product_scan = 0;
            //支付买家数
            int pay_user_num = 0;
            //支付转化率
            decimal pay_user_rate = 0;

            #region 多规格的情况下以||分割
            //商城原价
            string product_sale_price = "";
            //当前折扣价
            string product_preferential_price = "";
            //上架规格
            string product_specification = "";
            #endregion

            //促销方式
            string product_market_name = "";
            //上架时间
            string product_create_time = "";
            //产地
            string product_origin = "";

            #region 核心指标
            if (plateformType == "-1")
            {
                #region 全部
                List<object> tempBBC = BBCSingleProductData(productId, beginTime, endTime);
                if (syjNo > 0)
                {
                    List<object> tempSYJ = SYJSingleProductData(syjNo, beginTime, endTime);

                    single_product_total = tempBBC[0].ConToDecimal() + tempSYJ[0].ConToDecimal();

                    single_product_bbc_count = tempBBC[1].ConToInt();
                    single_product_syj_count= tempSYJ[1].ConToInt();
                    single_product_syj_unit = tempSYJ[4].ToString();

                    single_product_store = tempBBC[2].ConToInt() + tempSYJ[2].ConToInt();
                    single_product_order_count = tempBBC[3].ConToInt() + tempSYJ[3].ConToInt();
                }
                else
                {
                    single_product_total = tempBBC[0].ConToDecimal();
                    single_product_bbc_count = tempBBC[1].ConToInt();
                    single_product_store = tempBBC[2].ConToInt();
                    single_product_order_count = tempBBC[3].ConToInt();
                }

                single_product_scan = BBCSingleProductScanNum(productId, beginTime, endTime);

                pay_user_num = BBCSingleProductPayUserNum(productId, beginTime, endTime);
                if (single_product_scan > 0)
                {
                    pay_user_rate = Math.Round(pay_user_num.ConToDecimal() / single_product_scan.ConToDecimal(), 4) * 100;
                }
                //支付转化率 = 支付买家数 / 访客数;

                product_origin = GetProductOrigin(productId);

                #region 当前数据
                List<BBCReturnNowData> nowDataList = BBCSingleProductNowData(productId);
                if (nowDataList != null && nowDataList.Count > 0)
                {
                    product_market_name = nowDataList[0].marketName;
                    product_create_time = nowDataList[0].createTime.ToString("yyyy-MM-dd HH:mm:ss");
                }
                foreach (BBCReturnNowData data in nowDataList)
                {
                    if (string.IsNullOrEmpty(product_sale_price))
                    {
                        product_sale_price = data.salePrice.ToString();
                    }
                    else
                    {
                        product_sale_price += "||" + data.salePrice.ToString();
                    }

                    if (string.IsNullOrEmpty(product_preferential_price))
                    {
                        product_preferential_price = data.preferentialPrice.ToString();
                    }
                    else
                    {
                        product_preferential_price += "||" + data.preferentialPrice.ToString();
                    }

                    if (string.IsNullOrEmpty(product_specification))
                    {
                        if (!string.IsNullOrEmpty(data.specification))
                        {
                            product_specification = data.specification.ToString();
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(data.specification))
                        {
                            product_specification += "||" + data.specification.ToString();
                        }
                    }
                }
                #endregion
                #endregion
            }
            else if (plateformType == "0")
            {
                #region 实体店
                List<object> tempSYJ = SYJSingleProductData(productId, beginTime, endTime);

                single_product_total = tempSYJ[0].ConToDecimal();
                single_product_syj_count = tempSYJ[1].ConToInt();
                single_product_syj_unit = tempSYJ[4].ToString();
                single_product_store = tempSYJ[2].ConToInt();
                single_product_order_count = tempSYJ[3].ConToInt();
                #endregion
            }
            else if (plateformType == "1")
            {
                #region 电商
                List<object> tempBBC = BBCSingleProductData(productId, beginTime, endTime);

                single_product_total = tempBBC[0].ConToDecimal();
                single_product_bbc_count = tempBBC[1].ConToInt();
                single_product_store = tempBBC[2].ConToInt();
                single_product_order_count = tempBBC[3].ConToInt();

                single_product_scan = BBCSingleProductScanNum(productId, beginTime, endTime);

                pay_user_num = BBCSingleProductPayUserNum(productId, beginTime, endTime);
                if (single_product_scan > 0)
                {
                    pay_user_rate = Math.Round(pay_user_num.ConToDecimal() / single_product_scan.ConToDecimal(), 4) * 100;
                }

                product_origin = GetProductOrigin(productId);

                #region 当前数据
                List<BBCReturnNowData> nowDataList = BBCSingleProductNowData(productId);
                if (nowDataList != null && nowDataList.Count > 0)
                {
                    product_market_name = nowDataList[0].marketName;
                    product_create_time = nowDataList[0].createTime.ToString("yyyy-MM-dd HH:mm:ss");
                }
                foreach (BBCReturnNowData data in nowDataList)
                {
                    if (string.IsNullOrEmpty(product_sale_price))
                    {
                        product_sale_price = data.salePrice.ToString();
                    }
                    else
                    {
                        product_sale_price += "||" + data.salePrice.ToString();
                    }

                    if (string.IsNullOrEmpty(product_preferential_price))
                    {
                        product_preferential_price = data.preferentialPrice.ToString();
                    }
                    else
                    {
                        product_preferential_price += "||" + data.preferentialPrice.ToString();
                    }

                    if (string.IsNullOrEmpty(product_specification))
                    {
                        if (!string.IsNullOrEmpty(data.specification))
                        {
                            product_specification = data.specification.ToString();
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(data.specification))
                        {
                            product_specification += "||" + data.specification.ToString();
                        }
                    }
                }
                #endregion

                #endregion
            }
            #endregion


            if (single_product_order_count > 0)
            {
                single_product_cost_per_order = Math.Round(single_product_total / single_product_order_count, 2);
            }

            ViewBag.single_product_total = single_product_total;

            ViewBag.single_product_bbc_count = single_product_bbc_count;
            ViewBag.single_product_syj_count = single_product_syj_count;
            ViewBag.single_product_syj_unit = single_product_syj_unit;

            ViewBag.single_product_order_count = single_product_order_count;
            ViewBag.single_product_cost_per_order = single_product_cost_per_order;


            ViewBag.single_product_store = single_product_store;
            ViewBag.single_product_scan = single_product_scan;
            ViewBag.pay_user_num = pay_user_num;
            ViewBag.pay_user_rate = pay_user_rate;
            ViewBag.product_sale_price = product_sale_price;
            ViewBag.product_preferential_price = product_preferential_price;
            ViewBag.product_specification = product_specification;
            ViewBag.product_market_name = product_market_name;
            ViewBag.product_create_time = product_create_time;
            ViewBag.product_origin = product_origin;

            return View();
        }

        /// <summary>
        /// echart图表
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="plateformType"></param>
        /// <param name="syjNo"></param>
        /// <returns></returns>
        public JsonResult GetProductDetailIncome(long productId, string beginTime, string endTime, string plateformType, int syjNo=0)
        {
            beginTime = Extend.ConToBeginTime(beginTime);
            endTime = Extend.ConToEndTime(endTime);

            Result<BarOrLineModel> result = new Result<BarOrLineModel>();

            List<string> legendList = new List<string>();
            List<string> nameList = new List<string>();
            List<List<decimal>> dataList = new List<List<decimal>>();

            DateTime now = DateTime.Now;
            DateTime dateTemp = beginTime.ConToDateTime(now);
            DateTime dateTemp2 = endTime.ConToDateTime(now);
            bool isToday = dateTemp.ToShortDateString() == dateTemp2.ToShortDateString();
            string msg = "";

            if (isToday)
            {
                msg = "1";
                #region 今日数据
                List<HourDetailModel> detailList = new List<HourDetailModel>();

                if (plateformType == "-1")
                {
                    List<TimeAndIncome> allList = new List<TimeAndIncome>();
                    List<TimeAndIncome> bbcList = BBCHourTendency(beginTime, endTime, productId);
                    allList.AddRange(bbcList);

                    if (syjNo > 0)
                    {
                        List<TimeAndIncome> syjList = SYJHourTendency(beginTime, endTime, syjNo);
                        foreach (TimeAndIncome incomeSYJ in syjList)
                        {
                            List<string> temp = allList.Select(x => x.timeStr).ToList();

                            if (temp.Contains(incomeSYJ.timeStr))
                            {
                                TimeAndIncome target = allList.Where(x => x.timeStr == incomeSYJ.timeStr).First();
                                target.income += incomeSYJ.income;
                            }
                            else
                            {
                                allList.Add(incomeSYJ);
                            }
                        }
                    }

                    List<TimeAndIncome> allList2 = new List<TimeAndIncome>();
                    List<TimeAndIncome> bbcList2 = BBCHourTendency(beginTime, endTime, productId, true);
                    allList2.AddRange(bbcList2);

                    if (syjNo > 0)
                    {
                        List<TimeAndIncome> syjList2 = SYJHourTendency(beginTime, endTime, syjNo, true);
                        foreach (TimeAndIncome incomeSYJ2 in syjList2)
                        {
                            List<string> temp2 = allList2.Select(x => x.timeStr).ToList();

                            if (temp2.Contains(incomeSYJ2.timeStr))
                            {
                                TimeAndIncome target2 = allList2.Where(x => x.timeStr == incomeSYJ2.timeStr).First();
                                target2.income += incomeSYJ2.income;
                            }
                            else
                            {
                                allList2.Add(incomeSYJ2);
                            }
                        }
                    }


                    detailList = CaculateHourRate(allList, allList2);

                }
                else if (plateformType == "0")
                {
                    List<TimeAndIncome> syjList = SYJHourTendency(beginTime,endTime,productId);
                    List<TimeAndIncome> syjList2 = SYJHourTendency(beginTime, endTime, productId, true);

                    detailList = CaculateHourRate(syjList, syjList2);
                }
                else if (plateformType == "1")
                {
                    List<TimeAndIncome> bbcList = BBCHourTendency(beginTime, endTime, productId);
                    List<TimeAndIncome> bbcList2 = BBCHourTendency(beginTime, endTime, productId, true);
                    detailList = CaculateHourRate(bbcList, bbcList2);
                }

                #endregion

                legendList.Add("昨日销售额");
                legendList.Add("今日销售额");

                List<decimal> totalNowList = new List<decimal>();
                List<decimal> totalBeforeList = new List<decimal>();


                foreach (ProductController.HourDetailModel obj in detailList)
                {
                    nameList.Add(obj.timeStr);
                    totalNowList.Add(obj.incomeNow);
                    totalBeforeList.Add(obj.incomeBefore);
                }

                dataList.Add(totalBeforeList);
                dataList.Add(totalNowList);
            }
            else
            {
                msg = "0";
                #region 日详细数据
                List<DayDetailModel> detailList = new List<DayDetailModel>();

                if (plateformType == "-1")
                {
                    List<TimeAndIncome> allList = new List<TimeAndIncome>();
                    List<TimeAndIncome> bbcList = BBCDayTendency(productId, beginTime, endTime);
                    allList.AddRange(bbcList);

                    if (syjNo > 0)
                    {
                        List<TimeAndIncome> syjList = SYJDayTendency(syjNo, beginTime, endTime);
                        foreach (TimeAndIncome incomeSYJ in syjList)
                        {
                            List<string> temp = allList.Select(x => x.timeStr).ToList();

                            if (temp.Contains(incomeSYJ.timeStr))
                            {
                                TimeAndIncome target = allList.Where(x => x.timeStr == incomeSYJ.timeStr).First();
                                target.income += incomeSYJ.income;
                            }
                            else
                            {
                                allList.Add(incomeSYJ);
                            }
                        }
                    }

                    detailList = CaculateDayRate(allList, beginTime, endTime);

                }
                else if (plateformType == "0")
                {
                    List<TimeAndIncome> syjList = SYJDayTendency(productId, beginTime, endTime);
                    detailList = CaculateDayRate(syjList, beginTime, endTime);
                }
                else if (plateformType == "1")
                {
                    List<TimeAndIncome> bbcList = BBCDayTendency(productId, beginTime, endTime);
                    detailList = CaculateDayRate(bbcList, beginTime, endTime);
                }
                #endregion

                legendList.Add("销售额");
                legendList.Add("增长率");

                List<decimal> totalList = new List<decimal>();
                List<decimal> rateList = new List<decimal>();

                foreach (ProductController.DayDetailModel obj in detailList)
                {
                    nameList.Add(obj.timeStr);
                    totalList.Add(obj.income);
                    rateList.Add(obj.rate);
                }
                dataList.Add(totalList);
                dataList.Add(rateList);
            }

            BarOrLineModel bar = new BarOrLineModel();
            bar.NameList = nameList;
            bar.LegendList = legendList;
            bar.DataList = dataList;

            result.Status = 200;
            result.Body = bar;
            result.Msg = msg;

            DateTime end = DateTime.Now;
            LogHelper.WriteInfoLog("商品详情页结束时间=" + end.ToString("yyyy-MM-dd HH:mm:ss"));

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        #region detail查询所需类
        /// <summary>
        /// 获取产地
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        private string GetProductOrigin(long productId)
        {
            string sql = "select sp.provinceName+sc.cityName as origin from product as p,s_province as sp,s_city as sc where p.id="+productId+" and  p.provinceId=sp.provinceId and p.cityId=sc.cityId";
            var temp= BllBBCCommon.SelectSingleValueBySql("", sql);
            string origin = temp==null?"":temp.ToString();
            if (origin.Contains("北京市"))
            {
                origin = "北京市";
            }
            if (origin.Contains("上海市"))
            {
                origin = "上海市";
            }
            if (origin.Contains("重庆市"))
            {
                origin = "重庆市";
            } if (origin.Contains("天津市"))
            {
                origin = "天津市";
            }

            return origin;
        }

        /// <summary>
        /// 转换model
        /// </summary>
        /// <param name="bbcList"></param>
        /// <returns></returns>
        private List<DayDetailModel> CaculateDayRate(List<TimeAndIncome> bbcList,string beginTime,string endTime)
        {
          DateTime beginTime_ =  beginTime.ConToDateTime(DateTime.Now);
          DateTime endTime_=  endTime.ConToDateTime(DateTime.Now);
            DateTime start = Convert.ToDateTime(beginTime_.ToShortDateString());
            DateTime end = Convert.ToDateTime(endTime_.ToShortDateString());
            TimeSpan sp = end.Subtract(start);
            int dayNum = sp.Days;

            for (int i = 0; i <= dayNum; i++)
            {
              string temp= beginTime_.AddDays(i).ToString("yyyy-MM-dd");

              bool flag=   bbcList.Select(x => x.timeStr).Contains(temp);
              if (!flag)
              {
                  TimeAndIncome income = new TimeAndIncome();
                  income.timeStr = temp;
                  income.income = 0;

                  bbcList.Add(income);
              }

            }

            //按日期排序
            bbcList = bbcList.OrderBy(x => x.timeStr).ToList();

            List<DayDetailModel> detailList = new List<DayDetailModel>();
            decimal beforeIncome = 0;
            foreach (TimeAndIncome incomeBBC in bbcList)
            {
                DayDetailModel model = new DayDetailModel();
                model.timeStr = incomeBBC.timeStr;
                model.income = incomeBBC.income;
                if (beforeIncome > 0)
                {
                    model.rate = Math.Round((model.income - beforeIncome) / beforeIncome, 4) * 100;
                }
                else
                {
                    model.rate = 0;
                }


                beforeIncome = incomeBBC.income;
                detailList.Add(model);
            }

            return detailList;

        }

        /// <summary>
        /// BBC日趋势
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        private List<TimeAndIncome> BBCDayTendency(long productId, string beginTime, string endTime)
        {
            List<TimeAndIncome> tendencyList = new List<TimeAndIncome>();

            string sql_product_online_day_income = "select convert(char(10), o.payTime, 120) as timeStr, isnull(sum(od.totalMoney),0) as income from orders as o,orderDetail as od where o.id=od.orderId and od.productId=" + productId + " and o.payStatusId=2 and o.payTime>='" + beginTime + "' and o.payTime<='" + endTime + "' group by convert(char(10), o.payTime, 120)";
            tendencyList = BllBBCCommon.SelectEntityBySql<TimeAndIncome>("", sql_product_online_day_income);

            return tendencyList;
        }
        /// <summary>
        /// SYJ日趋势
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        private List<TimeAndIncome> SYJDayTendency(long productId, string beginTime, string endTime)
        {
            List<TimeAndIncome> tendencyList = new List<TimeAndIncome>();

            string sql_product_shop_day_income = "select convert(char(10), sd.ct, 120) as timeStr, isnull(sum(sd.dcpayment-sd.dcchange),0) as income from t_pos_syd as sd,t_pos_sys as ss where sd.s#=ss.d# and ss.p#=" + productId + " and sd.ct>='" + beginTime + "' and sd.ct<='" + endTime + "' group by convert(char(10), sd.ct, 120)";
            tendencyList = BllSYJCommon.SelectEntityBySql<TimeAndIncome>("", sql_product_shop_day_income);

            return tendencyList;
        }



        /// <summary>
        /// 转换model
        /// </summary>
        /// <param name="incomeNowList"></param>
        /// <param name="incomeBeforeList"></param>
        /// <returns></returns>
        private List<HourDetailModel> CaculateHourRate(List<TimeAndIncome> incomeNowList, List<TimeAndIncome> incomeBeforeList)
        {
            #region 转换成24小时制度
            for (int i = 0; i < 24; i++)
            {
                string timeStr = "";
                if (i < 10)
                {
                    timeStr = "0" + i.ToString();
                }
                else
                {
                    timeStr = i.ToString();
                }

                bool flag = incomeNowList.Select(x => x.timeStr).Contains(timeStr);
               if (!flag)
               {
                   TimeAndIncome income = new TimeAndIncome();
                   income.timeStr = timeStr;
                   income.income = 0;
                   incomeNowList.Add(income);
               }
               
            }
            #endregion

            //正向排序
            incomeNowList=incomeNowList.OrderBy(x=>x.timeStr.ConToInt()).ToList();

            List<HourDetailModel> detailList = new List<HourDetailModel>();
            foreach (TimeAndIncome incomeNow in incomeNowList)
            {
                int temp=incomeNow.timeStr.ConToInt() + 1;
                string next = temp < 10 ? "0" + temp : temp.ToString();
                HourDetailModel model = new HourDetailModel();
                model.timeStr = incomeNow.timeStr + ":00" + "-" + next + ":00";
                model.incomeNow = incomeNow.income;
                TimeAndIncome before = incomeBeforeList.Where(x => x.timeStr == incomeNow.timeStr).FirstOrDefault();
                if (before != null)
                {
                    model.incomeBefore = before.income;
                }
                else
                {
                    model.incomeBefore = 0;
                }
                if (model.incomeBefore != 0)
                {
                    model.rate = Math.Round((incomeNow.income - model.incomeBefore) / model.incomeBefore, 4) * 100;
                }
                else
                {
                    model.rate = 0;
                }
                detailList.Add(model);
            }

            return detailList;

        }

        /// <summary>
        /// BBC日趋势
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        private List<TimeAndIncome> BBCHourTendency(string beginTime,string endTime,long productId, bool isYestoday = false)
        {
            string beginTime_ = beginTime.ConToDateTime(DateTime.Now).AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
            string endTime_ = endTime.ConToDateTime(DateTime.Now).AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
            List<TimeAndIncome> tendencyList = new List<TimeAndIncome>();

            string sql_product_online_hour_income = "";
            if (isYestoday)
            {
                sql_product_online_hour_income = "select convert(char(2), o.payTime, 108) as timeStr, isnull(sum(od.totalMoney),0) as income from orders as o,orderDetail as od where o.id=od.orderId and od.productId=" + productId + " and o.payStatusId=2 and o.payTime>='"+beginTime_+"' and o.payTime<'"+endTime_+"' group by convert(char(2), o.payTime, 108)";
            }
            else
            {
                sql_product_online_hour_income = "select convert(char(2), o.payTime, 108) as timeStr, isnull(sum(od.totalMoney),0) as income from orders as o,orderDetail as od where o.id=od.orderId and od.productId=" + productId + " and o.payStatusId=2 and o.payTime>='" + beginTime + "' and o.payTime<'" + endTime + "' group by convert(char(2), o.payTime, 108)";
            }
            tendencyList = BllBBCCommon.SelectEntityBySql<TimeAndIncome>("", sql_product_online_hour_income);

            return tendencyList;
        }
        /// <summary>
        /// SYJ日趋势
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        private List<TimeAndIncome> SYJHourTendency(string beginTime, string endTime, long productId, bool isYestoday = false)
        {
            string beginTime_ = beginTime.ConToDateTime(DateTime.Now).AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
            string endTime_ = endTime.ConToDateTime(DateTime.Now).AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
            List<TimeAndIncome> tendencyList = new List<TimeAndIncome>();

            string sql_product_shop_hour_income = "";
            if (isYestoday)
            {
                sql_product_shop_hour_income = "select convert(char(2), sd.ct, 108) as timeStr, isnull(sum(sd.dcpayment-sd.dcchange),0) as income from t_pos_syd as sd,t_pos_sys as ss where sd.s#=ss.d# and ss.p#=" + productId + " and sd.ct>='"+beginTime_+"' and sd.ct<'"+endTime_+"' group by convert(char(2), sd.ct, 108)";
            }
            else
            {
                sql_product_shop_hour_income = "select convert(char(2), sd.ct, 108) as timeStr, isnull(sum(sd.dcpayment-sd.dcchange),0) as income from t_pos_syd as sd,t_pos_sys as ss where sd.s#=ss.d# and ss.p#=" + productId + " and sd.ct>='" + beginTime + "' and sd.ct<'" + endTime + "' group by convert(char(2), sd.ct, 108)";
            }
            tendencyList = BllSYJCommon.SelectEntityBySql<TimeAndIncome>("", sql_product_shop_hour_income);

            return tendencyList;
        }

        private class TimeAndIncome
        {
            public string timeStr { get; set; }

            public decimal income { get; set; }
        }

        public class DayDetailModel
        {
            public string timeStr { get; set; }

            public decimal income { get; set; }
            /// <summary>
            /// 较前一日上涨百分比
            /// </summary>
            public decimal rate { get; set; }
            /// <summary>
            /// 退款单数量
            /// </summary>
            public long returnNum { get; set; }
        }

        public class HourDetailModel
        {
            public string timeStr { get; set; }

            public decimal incomeNow { get; set; }

            public decimal incomeBefore { get; set; }
            /// <summary>
            /// 较前一日上涨百分比
            /// </summary>
            public decimal rate { get; set; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns>销售金额,销售数量,线上库存,订单笔数,销售单位</returns>
        private List<object> BBCSingleProductData(long productId, string beginTime, string endTime)
        {
            //销售金额
            string sql_bbc_single_product_total = "select isnull(sum(o.realTotal),0) as total from orders as o,orderDetail as od where o.id=od.orderId and o.payStatusId=2 and  o.payTime>='" + beginTime + "' and o.payTime<='" + endTime + "' and od.productId=" + productId;
            //销售数量
            string sql_bbc_single_product_amount = "select isnull(sum(od.amount),0) as amount from orders as o,orderDetail as od where o.id=od.orderId and o.payStatusId=2 and  o.payTime>='" + beginTime + "' and o.payTime<='" + endTime + "' and od.productId=" + productId;
            decimal bbcProductTotal = BllBBCCommon.SelectSingleValueBySql("", sql_bbc_single_product_total, "decimal").ConToDecimal();
            int bbcProductAmount = BllBBCCommon.SelectSingleValueBySql("", sql_bbc_single_product_amount, "int").ConToInt();
            //订单笔数
            string sql_bbc_single_product_count = "select cast(count(od.productId) as int) as amount from orders as o,orderDetail as od where o.id=od.orderId and o.payStatusId=2 and  o.payTime>='" + beginTime + "' and o.payTime<='" + endTime + "' and od.productId= " + productId;
            int bbcProductCount = BllBBCCommon.SelectSingleValueBySql("", sql_bbc_single_product_count, "int").ConToInt();

            //线上库存
            string sql_bbc_single_product_code = "select articlenumber from product where id=" + productId;
            var temp=BllBBCCommon.SelectSingleValueBySql("", sql_bbc_single_product_code);
            string code = temp==null?"":temp.ToString();
            string sql_erp_single_product_store = "select r.nums from is_room_info as r,is_sp_info as s where r.sp_id=s.id and s.`code`='" + code + "'";
            decimal storeNum_ = BllERPCommon.SelectSingleValueBySql("", sql_erp_single_product_store, "decimal").ConToDecimal();
            int storeNum = Math.Round(storeNum_,0).ConToInt();

            List<object> list = new List<object>();
            list.Add(bbcProductTotal);
            list.Add(bbcProductAmount);
            list.Add(storeNum);
            list.Add(bbcProductCount);

            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns>销售金额,销售数量,线下库存,订单笔数,销售单位</returns>
        private List<object> SYJSingleProductData(long productId, string beginTime, string endTime)
        {
            //销售金额
            string sql_syj_single_product_total = "select isnull(sum(sd.dcpayment-sd.dcchange),0) as total from t_pos_syd as sd,t_pos_sys as ss where sd.s#=ss.d# and  sd.ct>='" + beginTime + "' and sd.ct<='" + endTime + "' and ss.p#=" + productId;
            //销售数量
            string sql_syj_single_product_amount = "select isnull(sum(ss.sl),0) as amount from t_pos_syd as sd,t_pos_sys as ss where sd.s#=ss.d# and  sd.ct>='" + beginTime + "' and sd.ct<='" + endTime + "' and ss.p#=" + productId;
            decimal syjProductTotal = BllSYJCommon.SelectSingleValueBySql("", sql_syj_single_product_total, "decimal").ConToDecimal();
            decimal syjProductAmount_ = BllSYJCommon.SelectSingleValueBySql("", sql_syj_single_product_amount, "decimal").ConToDecimal();
            int syjProductAmount= Math.Round(syjProductAmount_,0).ConToInt();
            //销售单位
            string sql_syj_single_product_unit = "select dw from t_item_info where sx#="+productId;
            var temp=BllSYJCommon.SelectSingleValueBySql("t_item_info", sql_syj_single_product_unit);
            string syjProductUnit =temp==null?"":temp.ToString();
            //订单笔数
            string sql_syj_single_product_count = "select cast(count(distinct(ss.d#)) as int) as amount from t_pos_syd as sd,t_pos_sys as ss where sd.s#=ss.d# and  sd.ct>='" + beginTime + "' and sd.ct<='" + endTime + "' and ss.p#=" + productId;
            int syjProductCount = BllSYJCommon.SelectSingleValueBySql("", sql_syj_single_product_count, "int").ConToInt();
            //线下库存
            string sql_syj_single_product_store = "select Cast(s.yl as int) as storeNum from t_item_storage as s,t_item_info as f where s.ks#=f.sx# and s.ks#=" + productId;
            int storeNum = BllSYJCommon.SelectSingleValueBySql("", sql_syj_single_product_store, "int").ConToInt();

            List<object> list = new List<object>();
            list.Add(syjProductTotal);
            list.Add(syjProductAmount);
            list.Add(storeNum);
            list.Add(syjProductCount);
            list.Add(syjProductUnit);

            return list;
        }

        /// <summary>
        /// 浏览量
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        private int BBCSingleProductScanNum(long productId, string beginTime, string endTime)
        {
            int scanNum = 0;
            string sql_scan_num = "select count(1) as num from BrowseRecord where productId="+productId+" and createTime>='" + beginTime + "' and createTime<='" + endTime + "'";
            scanNum = BllBBCCommon.GetCountBySQL("BrowseRecord", sql_scan_num);

            return scanNum;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns>支付的买家数</returns>
        private int BBCSingleProductPayUserNum(long productId, string beginTime, string endTime)
        {
            int pay_user_num = 0;
            string sql_pay_user_num = "select count(od.productId) as amount from orders as o,orderDetail as od where o.id=od.orderId and o.payStatusId=2 and  o.payTime>='" + beginTime + "' and o.payTime<='" + endTime + "' and od.productId=" + productId;
            pay_user_num = BllBBCCommon.GetCountBySQL("orders", sql_pay_user_num);

            return pay_user_num;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        private List<BBCReturnNowData> BBCSingleProductNowData(long productId)
        {
            //促销方式
            string marketName = "";

            string sql = "select sku.salePrice, sku.preferentialPrice,sku.marketingEventId,sku.createTime,STUFF((SELECT ','+specificationValueAlias FROM productSkuSpecification as x WHERE x.productSkuId=sku.id FOR XML PATH('')), 1, 1, '') AS specification  from productSku as sku where sku.productId=" + productId + " and sku.isDeleted=0";

            List<BBCSelectNowData> nowDataList = BllBBCCommon.SelectEntityBySql<BBCSelectNowData>("productSku", sql);

            long marketEventId = 0;
            if (nowDataList != null)
            {
                marketEventId = nowDataList[0].marketingEventId;
            }

            string sql2 = "declare @marketEventId bigint,@productId bigint " +
                           "declare @marketName varchar(30),@presellStatus bit,@isRushPurchase bit " +
                           "set @marketEventId=" + marketEventId + "; " +
                           "set @productId=" + productId + "; " +
                           "set @marketName='无'; "+
                           "if @marketEventId>0 " +
                           "begin " +
                           "  select @marketName=name from marketingEvent where id=@marketEventId; " +
                           "end " +
                           "else " +
                           "begin " +
                           "  select @presellStatus=presellStatus,@isRushPurchase=isRushPurchase from product where id=@productId " +
                           "  if @presellStatus=1 " +
                           " begin " +
                           "    set @marketName='预售'; " +
                           "  end " +
                           "  if @isRushPurchase=1 " +
                           "  begin " +
                           "     set @marketName='抢购'; " +
                           "  end " +
                           "end " +
                           "select @marketName";
            marketName = BllBBCCommon.SelectSingleValueBySql("", sql2).ToString();


            List<BBCReturnNowData> list = new List<BBCReturnNowData>();
            foreach (BBCSelectNowData data in nowDataList)
            {
                BBCReturnNowData returnData = new BBCReturnNowData();
                returnData.createTime = data.createTime;
                returnData.marketName = marketName;
                returnData.preferentialPrice = data.preferentialPrice;
                returnData.salePrice = data.salePrice;
                returnData.specification = data.specification;

                list.Add(returnData);
            }


            return list;
        }
        private class BBCSelectNowData
        {
            public decimal salePrice { get; set; }

            public decimal preferentialPrice { get; set; }

            public long marketingEventId { get; set; }

            public DateTime createTime { get; set; }

            public string specification { get; set; }
        }

        private class BBCReturnNowData
        {
            public decimal salePrice { get; set; }

            public decimal preferentialPrice { get; set; }

            public string marketName { get; set; }

            public DateTime createTime { get; set; }

            public string specification { get; set; }
        }
        #endregion
    }
}